package org.ww.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ww.common.utils.PageUtils;
import org.ww.common.utils.Query;
import org.ww.product.dao.CategoryBrandRelationDao;
import org.ww.product.entity.BrandEntity;
import org.ww.product.entity.CategoryBrandRelationEntity;
import org.ww.product.entity.CategoryEntity;
import org.ww.product.service.BrandService;
import org.ww.product.service.CategoryBrandRelationService;
import org.ww.product.service.CategoryService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetails(CategoryBrandRelationEntity categoryBrandRelation) {
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateCategoryName(Long catId, String name) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(CategoryBrandRelationEntity::getCatelogId,catId);
        updateWrapper.set(CategoryBrandRelationEntity::getCatelogName,name);
        baseMapper.update(null,updateWrapper);
    }

    @Override
    public void updateBrandName(Long brandId, String name) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(CategoryBrandRelationEntity::getBrandId,brandId);
        updateWrapper.set(CategoryBrandRelationEntity::getBrandName,name);
        baseMapper.update(null,updateWrapper);
    }

    @Override
    public List<BrandEntity> getBrandListByCatId(Long catId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(CategoryBrandRelationEntity::getCatelogId,catId);
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = baseMapper.selectList(wrapper);

        List<Long> brandIds = Optional.ofNullable(categoryBrandRelationEntities)
                .orElseGet(ArrayList::new)
                .stream()
                .map(CategoryBrandRelationEntity::getBrandId)
                .distinct()
                .collect(Collectors.toList());

        return brandService.selectByIds(brandIds);

    }

}